/*
 * @(#)Regex.java    Jul 15, 2010
 *
 * Copyright 2009 Nyber Shanghai, Inc. All rights reserved.
 */

package com.lab.util.regex;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Class description goes here.
 *
 * @author nickevin@gmail.com
 * @version Jul 15, 2010 2:52:09 PM
 */

public class RegexUtils {

    private RegexUtils() {}

    public static String getFilenameExtension(File file) {
        return getFilenameExtension(file.getName());
    }

    /**
     * 1. 以最后一个.开始, 且不存在空格的连续字符.
     * 2. 至少有3个字符结尾.
     * 3. 均为英文字符.
     * 
     *  符合两条者为合法扩展名.
     * 
     */
    public static String getFilenameExtension(String filename) {
        int idx = filename.lastIndexOf(".");
        String substring = filename.substring(idx);

        Matcher matcher = Pattern.compile("[.][a-zA-Z]{3,}$")
                .matcher(substring);

        String extension = "";
        while (matcher.find()) {
            extension = matcher.group();
        }

        /* if ("".equals(extension)) {
             throw new IllegalArgumentException("Illegal Filename Characters.");
         }*/

        return extension;
    }
}
